import os
import dify_helper
import pandas as pd
import argparse
import configparser

def run_dify(row:dict,query:str,dh:dify_helper.DifyHelper):
    paramters = {
        "gender":row["性别"],
        "age":str(row["年龄"]),
        "film_show":row["影像表现"],
        "film_diagnose":row["影像诊断"]
    }
    print(paramters)
    try:
        answer = dh.send_json_message(paramters,row["问题"])
    except Exception as ex:
        print(ex)
        answer = "解读失败"
    # print(answer)
    return answer

def run_dify_batch(data_excel_path:str,out_excel_path:str,dh:dify_helper.DifyHelper):
    data_df = pd.read_excel(data_excel_path)
    data_df['AI解读'] = data_df.apply(lambda x:run_dify(x,"开始解读",dh),axis=1)
    data_df.to_excel(out_excel_path)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="dify批量运行器")
    parser.add_argument('-rep',"--raw_excel_path", type = str, help = "原始excel文件地址")
    parser.add_argument('-oep',"--out_excel_path", type = str, help = "输出excel文件地址")
    args = parser.parse_args()
    config = configparser.ConfigParser()
    config.read("config.ini")
    print(config.sections())
    raw_excel_path = args.raw_excel_path
    out_excel_path = args.out_excel_path
    dh = dify_helper.DifyHelper(config['dify']['base_url'],config['dify']['api_key'])
    run_dify_batch(raw_excel_path,out_excel_path,dh)